# asterisk-phones.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

# This file is part of the CarrierClass.net project.
# For more information: http://carrierclass.net/
#
package CCNv2::Provision::Asterisk::phones;
use strict;
use warnings;
use base qw(CCNCore::Provision::Base);
use CCNv2::Parameters;
use CCN::Text;
use CCNCore::Scripting;

sub run()
{
    my $self = shift;
    return unless defined $self->p->{cp_member};

    # YYY Modify this to use the PHo${ENTERPRISE_id}m${NUMBER} patterns for non-local numbers.
    # In this case we only need to enumerate the Numbers that are in pools that use this cp_cluster,
    # not all the numbers in the enterprise.
    

    # Here we travel through all the lines in order to enumerate
    # the phone numbers (extensions).
    $self->sql_do_for( <<'SQL', $self->p->{cp_member},
        SELECT pk, dialplan_transfertovm FROM enterprise 
        WHERE pk IN (SELECT enterprise FROM pool WHERE cp_cluster = ?)
SQL
    sub
    {
        my $ENTERPRISE_pk = shift;

        my $ENTERPRISE_id = CCNv2::Parameters::enterprise_id($ENTERPRISE_pk);
        my $TRANSFER_TO_VM = shift;

        my %extconf = ();

        my $extconf = "[${ENTERPRISE_pk}-phones]\n";
        my $ringconf = "[${ENTERPRISE_pk}-ring]\n";

        $self->sql_do_for( <<'SQL', $ENTERPRISE_pk,
            SELECT "number", "treatement", "treatment_parameters" FROM extension WHERE enterprise = ?
SQL
        sub
        {
            my ($NUMBER,$TREATMENT,$treatment_parameters) = @_;
            
            $treatment_parameters = '' if not defined $treatment_parameters;

            $extconf .= <<EOT;
exten => ${NUMBER},1,Macro(treatment-${TREATMENT},\${EXTEN},$treatment_parameters)
EOT
            $ringconf .= <<EOT;
exten => ${NUMBER},1,Goto(dispatch,${ENTERPRISE_id}m\${EXTEN},1)
EOT

            if( defined $TRANSFER_TO_VM )
            {
                my $LEN_TRANSFER_TO_VM = length($TRANSFER_TO_VM);
                $extconf .= <<"EOT" ;
exten => ${TRANSFER_TO_VM}${NUMBER},1,Goto(dispatch,VMo${ENTERPRISE_id}m\${EXTEN:${LEN_TRANSFER_TO_VM}})
EOT
            }
        });

        $self->warn ("No phones defined in enterprise ${ENTERPRISE_pk}") if $extconf eq '';

        # YYY Modify this to use the PHo${ENTERPRISE_id}m${NUMBER} patterns for non-local numbers
        #     (and send them to enterprise-${ENTERPRISE_pk}-to-proxy).
        $extconf .= "exten => _X.,1,Goto(dispatch,PHo${ENTERPRISE_id}m\${EXTEN},1)\n";

        # This is used to route the (local) "PHo" patterns inbound (see from-proxy).
        $extconf .= "[${ENTERPRISE_id}-phones]\ninclude => ${ENTERPRISE_pk}-phones\n";

        foreach ("/etc/asterisk/config/clients/${ENTERPRISE_pk}/extensions.phones.conf")
        {
            CAT $_, $extconf.$ringconf;
            IF_CHANGED $_ => CCNv2::Scripting::AST('extensions reload');
        }
    }); # foreach @cp_enterprises
}
1;